<template>
    <div>
        <van-nav-bar :title=" aid ? '编辑地址' : '新增地址'" left-text="返回" left-arrow @click-left="back" />
    </div>
    <div>

        <van-address-edit 
      
        :area-list="areaList" 
        :show-delete="!!aid"
        show-set-default 
        show-search-result
        :search-result="searchResult" 
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        @save="onSave"
        @delete="onDelete" 
        @change-detail="onChangeDetail"  
     />

    </div>
</template>

<script>
import { areaList } from '@vant/area-data';
import { post,get } from '../request';
import { useRoute, useRouter } from 'vue-router';
import { ref } from 'vue';
import { showToast } from 'vant';

export default {
    setup() {


        

        const route = useRoute();
        const aid = route.query.id;
        console.log("pid", aid);
       

        //返回
        const back = () => {
            router.go(-1);
        }
        const router = useRouter();

        //新增地址

        const onSave = async (e) => {
            console.log(e);
            const res = await post("/addAddress", {
                tokenString: JSON.parse(localStorage.getItem('tokens')),
                name: e.name,
                tel: e.tel,
                province: e.province,
                city: e.city,
                county: e.county,
                addressDetail: e.addressDetail,
                areaCode: e.areaCode,
                postalCode: 514500,
                isDefault: Number(e.isDefault)
            })
            console.log("新增地址", res);
            if (res.data.code == 9000) {
                router.go(-1);
            }
        }
        
        //删除地址
        const onDelete = async (e) => {
            console.log(e);
            const res = await post("/deleteAddress", {
                tokenString: JSON.parse(localStorage.getItem('tokens')),
                aid: e.aid
            })
            console.log("删除地址", res);
            if (res.data.code == 9000) {
                router.go(-1);
            }
        }

        const findAddressByAid = async (aid) => {
            const res = await get('/findAddressByAid', {
                params: {
                    aid,
                    tokenString: JSON.parse(localStorage.getItem('tokens'))
                }
            })
            console.log("查询是否成功", res.data);
            if (res.data.code == 9000) {
                onChangeDetail(res.data.data);
            }
        }
        findAddressByAid(aid);
       

       
        //编辑地址
        const onChangeDetail = async (aid) => {
            console.log( "编辑地址",aid);
            const res = await post("/editAddress", {
                tokenString: JSON.parse(localStorage.getItem('tokens')),
                name: e.name,
                aid: e.aid,
                tel: e.tel,
                province: e.province,
                city: e.city,
                county: e.county,
                addressDetail: e.addressDetail,
                areaCode: e.areaCode,
                postalCode: 514500,
                isDefault: Number(e.isDefault)
            })
            console.log("编辑地址", res);
            if (res.data.code == 9000) {
                showToast(res.data.msg)
                onSave()
                findAddressByAid(e.aid)
            }
            onChangeDetail(aid)
           
        }
        // onSave()
        return {
            onSave,
            onDelete,
            areaList,
            back,
            onChangeDetail,
            findAddressByAid
        }
    }
}
</script>

<style lang="less" scoped></style>